package com.salesys.gr.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.tony.domain.User;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

public class FrmModifyInfo1 {

	protected Shell FrmModifyInfo1;
	private Text txtNickName;
	private Text txtEmail;
	private Text txtQq;
	private Text txtBankcard;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmModifyInfo1.open();
		FrmModifyInfo1.layout();
		while (!FrmModifyInfo1.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmModifyInfo1 = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmModifyInfo1.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == 27) {
					FrmModifyInfo1.close();
				}
			}
		});
		FrmModifyInfo1.setSize(300, 240);
		FrmModifyInfo1.setText("修改个人资料");
		// 窗口居中
		FrmModifyInfo1.setLocation(new FrmHelper().setLocationCenter(FrmModifyInfo1));

		Label lblNickName = new Label(FrmModifyInfo1, SWT.NONE);
		lblNickName.setBounds(40, 26, 52, 17);
		lblNickName.setText("姓　名：");

		Label lblEmail = new Label(FrmModifyInfo1, SWT.NONE);
		lblEmail.setBounds(40, 56, 52, 17);
		lblEmail.setText("邮　箱：");

		Label lblQq = new Label(FrmModifyInfo1, SWT.NONE);
		lblQq.setBounds(40, 86, 52, 17);
		lblQq.setText("QQ：");

		Label lblBankcard = new Label(FrmModifyInfo1, SWT.NONE);
		lblBankcard.setBounds(40, 116, 56, 17);
		lblBankcard.setText("银行账号：");

		txtNickName = new Text(FrmModifyInfo1, SWT.BORDER);
		txtNickName.setBounds(102, 25, 150, 20);

		txtEmail = new Text(FrmModifyInfo1, SWT.BORDER);
		txtEmail.setBounds(102, 55, 150, 20);

		txtQq = new Text(FrmModifyInfo1, SWT.BORDER);
		txtQq.setBounds(102, 85, 150, 20);

		txtBankcard = new Text(FrmModifyInfo1, SWT.BORDER);
		txtBankcard.setBounds(102, 115, 150, 20);

		LoginedUserHelper.fresh();

		if (LoginedUserHelper.getLoginedUserNickname() != null) {
			txtNickName.setText(LoginedUserHelper.getLoginedUserNickname());
		}
		if (LoginedUserHelper.getLoginedUserEmail() != null) {
			txtEmail.setText(LoginedUserHelper.getLoginedUserEmail());
		}
		if (LoginedUserHelper.getLoginedUserQq() != null) {
			txtQq.setText(LoginedUserHelper.getLoginedUserQq());
		}
		if (LoginedUserHelper.getLoginedUserBankcard() != null) {
			txtBankcard.setText(LoginedUserHelper.getLoginedUserBankcard());
		}

		// 修改个人资料
		Button btnModify = new Button(FrmModifyInfo1, SWT.NONE);
		btnModify.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					modifyInfo();
				}
			}
		});
		btnModify.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				modifyInfo();
			}
		});
		btnModify.setBounds(197, 170, 88, 30);
		btnModify.setText("修　改");

		Label lblTip = new Label(FrmModifyInfo1, SWT.NONE);
		lblTip.setBounds(23, 144, 262, 17);
		lblTip.setText("注：银行账号姓名必须与该系统账户姓名一致！");
	}

	public void modifyInfo() {
		User modifyInfoUser = new User(LoginedUserHelper.getLoginedUser());
		modifyInfoUser.setNickname(txtNickName.getText());
		modifyInfoUser.setEmail(txtEmail.getText());
		modifyInfoUser.setQq(txtQq.getText());
		modifyInfoUser.setBankcard(txtBankcard.getText());
		if (modifyInfoUser.getNickname() == "") {
			new MsgBox().errorTip(FrmModifyInfo1, "姓名不能为空");
		} else if (modifyInfoUser.getEmail() == "") {
			new MsgBox().errorTip(FrmModifyInfo1, "邮箱不能为空");
		} else {
			int affect = 0;
			try {
				affect = new UserService().modifyInfo(modifyInfoUser);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (affect == 0) {
				new MsgBox().errorTip(FrmModifyInfo1, "修改失败！");
			} else {
				new MsgBox().infoTip(FrmModifyInfo1, "修改成功！");
				LoginedUserHelper.fresh();
				FrmModifyInfo1.close();
			}
		}
	}
}
